<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:zxda:coreZxda:edit'">
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('新增') }}
        </a-button>
        <Popconfirm
          title="确认结项?"
          v-auth="'cardd:zxda:coreZxda:edit'"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleJx()"
        >
          <a-button type="primary" class="ant-btn-success">
            <CheckOutlined />
            {{ t('结项') }}
          </a-button>
        </Popconfirm>
        <Popconfirm
          v-auth="'cardd:zxda:coreZxda:edit'"
          title="确认取消结项?"
          ok-text="确认"
          @confirm="handleQxjx"
        >
          <a-button type="primary" class="ant-btn-error">
            <CloseOutlined />
            {{ t('取消结项') }}
          </a-button>
        </Popconfirm>
        <a-button type="primary" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" />
          {{ t('导出') }}
        </a-button>
        <a-button type="primary" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" />
          {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.zxdaZxbm }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddZxdaCoreZxdaList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { coreZxdaDelete, coreZxdaListData, coreZxdaEnd } from '/@/api/cardd/zxda/coreZxda';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { CheckOutlined, CloseOutlined } from '@ant-design/icons-vue';
  import { Popconfirm } from 'ant-design-vue';

  const { t } = useI18n('cardd.zxda.coreZxda');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('专项档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('专项编码'),
        field: 'zxdaZxbm',
        component: 'Input',
      },
      {
        label: t('专项名称'),
        field: 'zxdaZxmc',
        component: 'Input',
      },
      {
        label: t('立项日期'),
        field: 'zxdaLxrq',
        component: 'Input',
      },
      {
        label: t('对应客户'),
        field: 'zxdaDykh',
        component: 'Input',
      },
      {
        label: t('是否结项'),
        field: 'zxdaSfjx',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
        },
      },
      {
        label: t('结项日期'),
        field: 'zxdaJxrq',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('专项编码'),
      dataIndex: 'zxdaZxbm',
      key: 'a.zxda_zxbm',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('专项名称'),
      dataIndex: 'zxdaZxmc',
      key: 'a.zxda_zxmc',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('立项日期'),
      dataIndex: 'zxdaLxrq',
      key: 'a.zxda_lxrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('对应客户'),
      dataIndex: 'basCus.khdaDwqc',
      key: 'a.zxda_dykh',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('是否结项'),
      dataIndex: 'zxdaSfjx',
      dictType: 'sys_yes_no',
      key: 'a.zxda_sfjx',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('结项日期'),
      dataIndex: 'zxdaJxrq',
      key: 'a.zxda_jxrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑专项档案'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:zxda:coreZxda:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除专项档案'),
        popConfirm: {
          title: t('是否确认删除专项档案'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:zxda:coreZxda:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: coreZxdaListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/cardd/zxda/coreZxda/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await coreZxdaDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  async function handleJx() {
    if (getSelectRowKeys().length) {
      const res = await coreZxdaEnd({ selIds: getSelectRowKeys().join(','), zxdaSfjx: '1' });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选中要结项的档案！', 'error');
    }
  }

  async function handleQxjx() {
    if (getSelectRowKeys().length) {
      const res = await coreZxdaEnd({ selIds: getSelectRowKeys().join(','), zxdaSfjx: '0' });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选中要取消结项的档案！', 'error');
    }
  }
</script>
